package com.browser.security;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
public class CookieManager {
    private static final Logger logger = LoggerFactory.getLogger(CookieManager.class);
    private final Map<String, Map<String, Cookie>> cookies;

    @Data
    public static class Cookie {
        private final String name;
        private final String value;
        private final long expiry;
        private final boolean secure;
        private final boolean httpOnly;
    }

    public CookieManager() {
        this.cookies = new ConcurrentHashMap<>();
    }

    public void setCookie(String domain, Cookie cookie) {
        logger.debug("Setting cookie for domain: {}", domain);
        cookies.computeIfAbsent(domain, k -> new HashMap<>())
               .put(cookie.getName(), cookie);
    }

    public Cookie getCookie(String domain, String name) {
        Map<String, Cookie> domainCookies = cookies.get(domain);
        return domainCookies != null ? domainCookies.get(name) : null;
    }

    public void clearCookies(String domain) {
        cookies.remove(domain);
    }

    public void clearAllCookies() {
        cookies.clear();
    }
} 